package com.shire.shire_interface.service.marketing;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.shire.shire_interface.dao.accounts.WxAccountDao;
import com.shire.shire_interface.dao.group.WxGroupDao;
import com.shire.shire_interface.dao.marketing.WxActiveQrcodeDao;
import com.shire.shire_interface.dao.marketing.WxActiveQrcodeRecordDao;
import com.shire.shire_interface.model.group.WxGroup;
import com.shire.shire_interface.model.group.WxGroupExample;
import com.shire.shire_interface.model.marketing.WxActiveQrcode;
import com.shire.shire_interface.model.marketing.WxActiveQrcodeExample;
import com.shire.shire_interface.model.marketing.WxActiveQrcodeRecord;
import com.shire.shire_interface.model.marketing.WxActiveQrcodeRecordExample;
import com.shire.shire_interface.model.user.WxAccount;
import com.shire.shire_interface.model.user.WxAccountExample;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.*;

@Service
public class WxActiveQrcodeService {
    @Resource
    private WxActiveQrcodeDao activeQrcodeDao;
    @Resource
    private WxActiveQrcodeRecordDao activeQrcodeRecordDao;

    @Resource
    private WxGroupDao wxGroupDao;
    @Resource
    private WxAccountDao accountDao;

    public int saveWxActiveQrcode(WxActiveQrcode record){
        if (record.getId() == null){
            return activeQrcodeDao.insertSelective(record);
        }else {
            return activeQrcodeDao.updateByPrimaryKeySelective(record);
        }
    }

    public WxActiveQrcode selectQrcodeById(Integer id){
        WxActiveQrcode qrcode = activeQrcodeDao.selectByPrimaryKey(id);
        if (qrcode == null)
            return null;
        List<WxActiveQrcode> list = new ArrayList<>();
        list.add(qrcode);
        getGroupsForCode(list);
        return list.get(0);
    }

    public List<WxActiveQrcode> getQrCodeRecords(WxActiveQrcodeExample example){
        Date s = new Date();
        List<WxActiveQrcode> qrcodes = activeQrcodeDao.selectByExampleWithBLOBs(example);
        getGroupsForCode(qrcodes);
        Date e = new Date();

        System.out.println("use time ++++++++++ " + (e.getTime() - s.getTime()));

        return qrcodes;
    }

    private void getGroupsForCode(List<WxActiveQrcode> qrcodeList) {
        Map<String, List<String>> groupNoMap = new HashMap<>();
        List<String> accountIdList = new ArrayList<>();
        for (WxActiveQrcode qrcode : qrcodeList) {
            if (qrcode.getCodetype() == 1) {
                JSONArray jsonArray = JSONArray.parseArray(qrcode.getItemids());
                for (Object o : jsonArray) {
                    JSONObject object = (JSONObject) o;

                    String acount = object.getString("wxAccount");
                    String groupNO = object.getString("groupNo");
                    if (groupNoMap.containsKey(acount)) {
                        groupNoMap.get(acount).add(groupNO);
                    } else {
                        List<String> list = new ArrayList<>();
                        list.add(groupNO);
                        groupNoMap.put(acount, list);
                    }
                }
            } else {
                JSONArray jsonArray = JSONArray.parseArray(qrcode.getItemids());

                for (Object o : jsonArray) {
                    accountIdList.add((String) o);
                }
            }
        }

        Map<String, WxGroup> groupMap= new HashMap<>();

        if (groupNoMap.entrySet().size()>0) {
            WxGroupExample groupExample = new WxGroupExample();
            for (Map.Entry<String, List<String>> item : groupNoMap.entrySet()) {
                groupExample.or().andWxaccountEqualTo(item.getKey()).andGroupnoIn(item.getValue());
            }
            List<WxGroup> groupList = wxGroupDao.selectByExample(groupExample);
            for (WxGroup group : groupList)
                groupMap.put(group.getWxaccount() + group.getGroupno(), group);
        }


        Map<String, WxAccount> accountMap = new HashMap<>();
        if (accountIdList.size()>0) {
            WxAccountExample example = new WxAccountExample();
            example.createCriteria().andWxidIn(accountIdList);
            List<WxAccount> accountList = accountDao.selectByExampleWithBLOBs(example);

            for (WxAccount account : accountList) {
                accountMap.put(account.getWxid(), account);
            }
        }

        for (WxActiveQrcode qrcode : qrcodeList) {
            if (qrcode.getCodetype() == 1) {
                JSONArray jsonArray = JSONArray.parseArray(qrcode.getItemids());
                List<WxGroup> codeGroupList = new ArrayList<>();
                for (Object o : jsonArray) {
                    JSONObject object = (JSONObject) o;

                    String acount = object.getString("wxAccount");
                    String groupNO = object.getString("groupNo");
                    codeGroupList.add(groupMap.get(acount+groupNO));
                }
                qrcode.setGroups(codeGroupList);
            } else {
                JSONArray jsonArray = JSONArray.parseArray(qrcode.getItemids());
                List<WxAccount> codeAccountList = new ArrayList<>();

                for (Object o : jsonArray) {
                    codeAccountList.add(accountMap.get((String) o));
                }
                qrcode.setAccounts(codeAccountList);
            }
        }

    }

    public WxActiveQrcodeRecord getQrcodeRecord(Integer codeId, String fingerPrint){
        WxActiveQrcodeRecordExample example = new WxActiveQrcodeRecordExample();
        example.createCriteria().andQrcodeidEqualTo(codeId).andFigureidEqualTo(fingerPrint);
        List<WxActiveQrcodeRecord> records = activeQrcodeRecordDao.selectByExample(example);

        if (records.size()>0){
            return records.get(0);
        }
        return null;
    }

    public int inserQrcodeRecord(WxActiveQrcodeRecord record){
        record.setCreatetime(new Date());
        return activeQrcodeRecordDao.insert(record);
    }
}
